#include "systemc.h"
#include "random.h"
#include "structs.h"
#include "channel.h"

SC_MODULE(dmem)
{
//-------------in-out---------------------------------------------------------//
	sc_port< async_channel_in_if< exdm > > exdm_struct;
	sc_port< async_channel_out_if< sc_int<32> > > data_out;
	sc_port< async_channel_in_if< sc_int<32> > > data_in;
//----------------------------------------------------------------------------//

//-------------Variáveis-Locais-----------------------------------------------//
	exdm ExDm;
//----------------------------------------------------------------------------//

	void behavior()
	{
		sc_int<32> dmem[] = {
			0x00000000,
			0x00000000,
			0x00000000,
			0x00000000,
			0x00000000,
			0x00000000,
			0x00000000,
			0x00000000
		};

		while(true)
		{
			exdm_struct->read(ExDm);
// 			cout << "[DME] rw <-- " << ExDm.rw << " | @ " << sc_time_stamp() << endl;
			switch(ExDm.rw)
			{
				case 1: //read
// 					cout << "[DME] data_out --> " << dmem[ExDm.rd.range(2,0)] << " | @ " << sc_time_stamp() << endl;
					data_out->write(dmem[ExDm.rd.range(2,0)]);
					break;
				case 0: //write
					data_in->read(dmem[ExDm.rd.range(2,0)]);
// 					cout << "[DME] data_in <-- " << dmem[ExDm.rd.range(2,0)] << " | @ " << sc_time_stamp() << endl;
					break;
			}
		}
	}

	SC_CTOR(dmem)
	{
		SC_THREAD(behavior);
	}
};